﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System.IO;

/// <summary>
/// 打包工具
/// </summary>
public partial class BuildUtility
{
    #region 构建路径
    public static readonly string projectPath = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length);
    public static readonly string outputPath_Apk = projectPath + "build/apks/";
    public static readonly string outputPath_Android = "build/project_Android";
    public static readonly string outputPath_Ios = "build/project_Ios";
    #endregion

    /// <summary>
    /// 打包场景数组
    /// 需要根据项目修改
    /// </summary>
    public static readonly string[] buildScenes = new string[] {
        "Assets/_Test/Utility/BuildUtilityTest.unity"
    };

    [MenuItem("LitTools/打包工具/构建Apk")]
    public static void BuildApk()
    {
        //设置构建的目标平台为Android
        BuildTarget buildTarget = BuildTarget.Android;
        //设置构建的输出路径和文件名
        string apkName = outputPath_Apk + $"{Application.productName}_{buildTarget}_{DateTime.Now:yyyy_MM_dd_HH_mm_ss}.apk";

        // 构建Player
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
        buildPlayerOptions.scenes = buildScenes; // 指定要构建的场景
        buildPlayerOptions.locationPathName = apkName;
        buildPlayerOptions.target = buildTarget;
        buildPlayerOptions.options = BuildOptions.None;

        //删除上次构建结果
        Directory.Delete(outputPath_Apk, true);
        //创建构建目录
        Directory.CreateDirectory(outputPath_Apk);
        // 执行构建
        BuildPipeline.BuildPlayer(buildPlayerOptions);
    }

    public static void BuildWithGradle()
    {
        // 设置导出的目标平台为Android
        BuildTarget buildTarget = BuildTarget.Android;

        // 导出Android项目
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
        buildPlayerOptions.scenes = buildScenes;
        buildPlayerOptions.locationPathName = outputPath_Android;
        buildPlayerOptions.target = buildTarget;
        buildPlayerOptions.options = BuildOptions.AcceptExternalModificationsToPlayer;

        // 执行导出
        BuildPipeline.BuildPlayer(buildPlayerOptions);

        // 使用Gradle构建Android项目
        string gradlePath = "Path/To/Gradle"; // 指定Gradle的路径
        string gradleCommand = "assembleRelease"; // 指定Gradle的构建命令

        Process process = new Process();
        process.StartInfo.FileName = gradlePath;
        process.StartInfo.WorkingDirectory = outputPath_Android;
        process.StartInfo.Arguments = gradleCommand;
        process.Start();
        process.WaitForExit();
    }

}

/// <summary>
/// 打包流程监听
/// </summary>
public partial class BuildUtility : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
    public int callbackOrder => 0;

    /// <summary>
    /// 打包开始前
    /// </summary>
    /// <param name="report"></param>
    public void OnPreprocessBuild(BuildReport report)
    {
        
    }

    /// <summary>
    /// 打包结束后
    /// </summary>
    /// <param name="report"></param>
    public void OnPostprocessBuild(BuildReport report)
    {
        UnityEngine.Debug.Log(report.summary.result);
        // 检查构建是否成功
        if (report.summary.result == BuildResult.Succeeded)
        {
            // 设置打包输出路径
            string outputPath = report.summary.outputPath;
            // 打开文件夹
            Process.Start(outputPath);
        }
        else
        {
            UnityEngine.Debug.LogError("打包失败");
        }
    }


}

